package com.mach.platform.api.repository;

import com.mach.platform.api.domain.Message;
import com.mach.platform.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Repository
public interface MessageRepo extends BaseRepository<Message> {

    @Modifying
    @Transactional
    @Query(value = "update message set is_del = 1 where id = ?1", nativeQuery = true)
    int updateById(Long id);

    /**
     * 查询多个设备的报警，运维，点检次数
     * @param devIds
     * @return
     */
    @Query(value = "select dev_id, hurdles, count(*) num from message where dev_id in (?1) and type = 1 and hurdles in (1, 2, 3) group by dev_id, hurdles", nativeQuery = true)
    List<Map> getMaintainDataByDevIds(List<Long> devIds);

    /**
     * 查询单个设备的报警，运维，点检次数
     * @param devId
     * @return
     */
    @Query(value = "select hurdles, count(*) num from message where dev_id = ?1 and type = 1 and hurdles in (1, 2, 3) group by hurdles", nativeQuery = true)
    List<Map> getMaintainDataByDevId(Long devId);

}
